package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.Article;
import com.guochenglang.www.pojo.Traces;
import com.guochenglang.www.service.ArticleService;
import com.guochenglang.www.service.TracesService;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.view.vo.ArticleView;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 浏览记录Controller层
 *
 * @author GuoChenglang
 */
public class TracesController {
    private static final TracesService TRACES_SERVICE = MyDependency.returnObject(TracesService.class);
    private static final ArticleService ARTICLE_SERVICE = MyDependency.returnObject(ArticleService.class);

    /**
     * 添加浏览记录
     */
    public static Result insertTraces(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得用户id
        Integer userId = GetObject.getValue(request, Integer.class, "userId");
        //获得文章id
        Integer articleId = GetObject.getValue(request, Integer.class, "articleId");
        System.out.println(userId + "  " + articleId);
        int key = TRACES_SERVICE.insertTraces(new Traces(null, articleId, userId));
        return Result.result(key);
    }

    /**
     * 展示浏览记录
     */
    public static Result getTraces(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得用户id
        Integer userId = GetObject.getObject(request, Integer.class);
        //获得浏览记录列表
        ArrayList<Traces> traces = TRACES_SERVICE.selectTraces(userId);
        ArrayList<ArticleView> articleViews = new ArrayList<>();
        //根据id获得具体文章
        for (Traces trace : traces) {
            //封装vo层
            ArticleView articleView = new ArticleView();
            articleView.setTraces(trace);
            Article thisArticle = ARTICLE_SERVICE.getArticle(trace.getArticleId());
            articleView.setArticle(thisArticle);
            articleViews.add(articleView);
        }
        return Result.result(articleViews);
    }

    /**
     * 删除一个浏览记录
     */
    public static Result deleteATrace(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得浏览记录id
        Integer id = GetObject.getObject(request, Integer.class);
        int num = TRACES_SERVICE.deleteATraces(id);
        return Result.result(num);
    }

    /**
     * 删除所有浏览记录
     */
    public static Result deleteAll(HttpServletRequest request, HttpServletResponse response) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, IOException {
        //获得用户id
        Integer userId = GetObject.getObject(request, Integer.class);
        int num = TRACES_SERVICE.deleteAll(userId);
        return Result.result(num);
    }
}
